home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / packet / monax25 / totals.c < prev    next >
Text File  |  1987-10-28  |  8KB  |  359 lines

  1. /* totals.c - Report tool for use with STATS AX.25 monitor program.
  2.    This module is part of totals.exe
  3.                   
  4.    Language = Microsoft C version 4.0
  5.  
  6.  
  7.    This source is distributed freely and may be copied and
  8.    redistributed with the following provisos:
  9.    
  10.            You may not sell it, nor may you charge for making 
  11.            copies beyond the actual cost of mailing and media.
  12.                       
  13.    Written by Skip Hansen WB6YMH and Harold Price NK6K.
  14.  
  15.    Feedback is desired.
  16.  
  17.    RCP/M (213) 541-2503 300/1200/2400 baud
  18.    or via packet WB6YMH @ WB6YMH-2 or 
  19.          NK6K @ NK6K
  20.  
  21.    Modification history:
  22.  
  23.     8/10/87         NK6K: Initial release.    
  24.     ver 1.0         
  25.  
  26.    10/18/87     NK6K: First general release.
  27.    ver 1.1
  28.  
  29. */
  30.  
  31. #include <stdio.h>
  32. #include <search.h>
  33. #include <string.h>
  34. #include "monfile.h"
  35.  
  36.  
  37. /* configuration constants */
  38.  
  39. #define MAX_CIRCUIT     400    /* number of entrys in circuit table     */
  40. #define MAX_DIGIS     100    /* number of entrys in digi table     */
  41.  
  42. #define FALSE     0
  43. #define TRUE     !FALSE
  44.  
  45. struct TOTALS {
  46.     char call[10];
  47.     unsigned long rx_tbytes;        /* total bytes */
  48.     unsigned long rx_udbytes;        /* unique data bytes */
  49.     unsigned long rx_ndbytes;        /* non-digi data bytes */
  50.     unsigned long rx_tdbytes;        /* total data bytes */
  51.     unsigned long tx_tbytes;        /* total bytes */
  52.     unsigned long tx_udbytes;        /* unique data bytes */
  53.     unsigned long tx_ndbytes;        /* non-digi data bytes */
  54.     unsigned long tx_tdbytes;        /* total data bytes */
  55. } total[MAX_CIRCUIT];
  56.  
  57. struct DIGI_TOTALS{
  58.     char call[10];
  59.     unsigned long bytes;
  60.     unsigned long packets;
  61. } digi_tab[MAX_DIGIS];
  62.  
  63. struct FREQ_RECORD freq ={0,0,0,0,0,0,0,0,0,0,0};
  64.  
  65.  
  66. int    nr_digis = 0;
  67. int    nr_calls = 0;
  68.  
  69. int    dnum;
  70. int    cnum;
  71.  
  72. char    line[160];
  73.  
  74. int compare(),d_compare();
  75.  
  76. char *sel_call;
  77. int sel_flag=0;
  78. unsigned long recnum=1;
  79.  
  80. main (argc, argv)
  81. int    argc;
  82. char    *argv[];
  83. {
  84.     char c;
  85.     int i;
  86.  
  87.     if (argc==2) {
  88.         sel_flag=1;
  89.         sel_call = argv[1];
  90.         strupr(sel_call);
  91.         fprintf(stderr,"Collecting data for circuits including %s only.\n",
  92.             sel_call);
  93.         }
  94.  
  95.     fprintf(stderr,"Totaling ...\n");
  96.     while(!feof(stdin)){
  97.         switch(c = getc(stdin)){
  98.             case 'T':
  99.                 do_time();
  100.                 break;
  101.  
  102.             case 'F':
  103.                 do_freq();
  104.                 break;
  105.  
  106.             case 'C':
  107.                 do_circuit();
  108.                 break;
  109.  
  110.             case 'D':
  111.                 do_digi();
  112.                 break;
  113.  
  114.             default:
  115.                 fprintf(stderr,"Error: Unknown record type '%c'.\n",c);
  116.                 fprintf(stderr,"Line ignored.\n");
  117.                 break;
  118.         }
  119.     recnum++;
  120.     }
  121. /*    close(fp);*/
  122.     if (sel_flag) {
  123.         find_call(sel_call);
  124.         fprintf(stdout,"Z,%s,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld\n",
  125.             total[cnum].call,
  126.             total[cnum].rx_tbytes,
  127.             total[cnum].rx_udbytes,
  128.             total[cnum].rx_ndbytes,
  129.             total[cnum].rx_tdbytes,
  130.             total[cnum].tx_tbytes,
  131.             total[cnum].tx_udbytes,
  132.             total[cnum].tx_ndbytes,
  133.             total[cnum].tx_tdbytes);
  134.         }
  135.  
  136.     fprintf(stderr,"\rSorting ...    \n");
  137.     qsort(&total[0],nr_calls,sizeof(struct TOTALS),compare);
  138.     qsort(&digi_tab[0],nr_digis,sizeof(struct DIGI_TOTALS),d_compare);
  139.     fprintf(stderr,"Writing TOTAL file...\n");
  140. /*    if(!(fp = fopen("TOTAL","w"))){
  141.         printf("Fatal error: can't open TOTAL file\n");
  142.         exit();
  143.     }
  144. */
  145.     fprintf(stdout,"F,%lu,%lu,%lu,",freq.t_packets,freq.t_bytes,freq.u_packets);
  146.     fprintf(stdout,"%lu,%lu,%lu,",freq.u_bytes,freq.l32,freq.l64);
  147.     fprintf(stdout,"%lu,%lu,%lu,",freq.l128,freq.l256,freq.g256);
  148.     fprintf(stdout,"%lu,%lu\n",freq.dcd_on_ticks,freq.dcd_off_ticks);
  149.     for(i=0; i < nr_digis; i++){
  150.         fprintf(stdout,"D,%s,%ld,",digi_tab[i].call,digi_tab[i].bytes);
  151.         fprintf(stdout,"%ld\n",digi_tab[i].packets);
  152.     }
  153.  
  154.     for(i=0; i < nr_calls; i++){
  155.         if ((!sel_flag) || (strcmp(sel_call,total[i].call)!=0)) {
  156.           fprintf(stdout,"S,%s,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld\n",
  157.             total[i].call,
  158.             total[i].rx_tbytes,
  159.             total[i].rx_udbytes,
  160.             total[i].rx_ndbytes,
  161.             total[i].rx_tdbytes,
  162.             total[i].tx_tbytes,
  163.             total[i].tx_udbytes,
  164.             total[i].tx_ndbytes,
  165.             total[i].tx_tdbytes);
  166.             }
  167.  
  168.     }
  169. /*    fclose(fp);*/
  170. }
  171.  
  172. do_time()
  173. {
  174.     fgets(line,160,stdin);
  175. }
  176.  
  177. do_freq()
  178. {
  179.     unsigned long t_packets;    /* total packets */
  180.     unsigned long t_bytes;        /* total bytes */
  181.     unsigned long u_packets;    /* unique packets */
  182.     unsigned long u_bytes;        /* unique bytes */
  183.     unsigned long l32;        /* <=32 */
  184.     unsigned long l64;        /* <=64 */
  185.     unsigned long l128;        /* <=128 */
  186.     unsigned long l256;        /* <=256 */
  187.     unsigned long g256;        /* > 256 */
  188.     unsigned long dcd_on_ticks;    /* ticks when DCD was on */
  189.     unsigned long dcd_off_ticks;    /* ticks when DCD was off */
  190.  
  191.     fscanf(stdin,",%ld,%ld,%ld,%ld,",&t_packets,&t_bytes,&u_packets,&u_bytes);
  192.     fscanf(stdin,"%ld,%ld,%ld,%ld,%ld,",&l32,&l64,&l128,&l256,&g256);
  193.     fscanf(stdin,"%ld,%ld\n",&dcd_on_ticks,&dcd_off_ticks);
  194.     
  195.     freq.t_packets += t_packets;
  196.     freq.t_bytes += t_bytes;
  197.     freq.u_packets += u_packets;
  198.     freq.u_bytes += u_bytes;
  199.     freq.l32 += l32;
  200.     freq.l64 += l64;
  201.     freq.l128 += l128;
  202.     freq.l256 += l256;
  203.     freq.g256 += g256;
  204.     freq.dcd_on_ticks += dcd_on_ticks;
  205.     freq.dcd_off_ticks += dcd_off_ticks;
  206. }
  207.  
  208. do_circuit()
  209. {
  210.     char to[10],from[10];
  211.     int digis,pid;
  212.     unsigned long u_dpackets,nd_dpackets,t_dpackets;
  213.     unsigned long nd_packets,t_packets,u_dbytes;
  214.     unsigned long nd_dbytes,t_dbytes,nd_bytes,t_bytes;
  215.     unsigned long c_time,sabm,ua,disc,dm,rej,rr,rnr,i,ui;
  216.     unsigned long frmr,poll,final,l32,l64,l128,l256,g256;
  217.     
  218.     fscanf(stdin,",%[^,],%[^,],%d,",to,from,&digis);
  219.     fscanf(stdin,"%d,",&pid);
  220.     fscanf(stdin,"%lu,%lu,",&u_dpackets,&nd_dpackets);
  221.     fscanf(stdin,"%lu,%lu,",&t_dpackets,&nd_packets);
  222.     fscanf(stdin,"%lu,%lu,",&t_packets,&u_dbytes);
  223.     fscanf(stdin,"%lu,",&nd_dbytes);
  224.     fscanf(stdin,"%lu,%lu,",&t_dbytes,&nd_bytes);
  225.     fscanf(stdin,"%lu,",&t_bytes);
  226.     fscanf(stdin,"%lu,",&c_time);
  227.     fscanf(stdin,"%lu,%lu,%lu,",&sabm,&ua,&disc);
  228.     fscanf(stdin,"%lu,%lu,%lu,",&dm,&rej,&rr);
  229.     fscanf(stdin,"%lu,%lu,",&rnr,&i);
  230.     fscanf(stdin,"%lu,%lu,",&ui,&frmr);
  231.     fscanf(stdin,"%lu,%lu,%lu,",&poll,&final,&l32);
  232.     fscanf(stdin,"%lu,%lu,%lu,",&l64,&l128,&l256);
  233.     fscanf(stdin,"%lu\n",&g256);
  234.  
  235.     if (sel_flag) {
  236.         if ((strcmp(sel_call,to)!=0) && (strcmp(sel_call,from)!=0))
  237.             return;
  238.         }
  239.  
  240.     if (t_dbytes > t_bytes) {
  241.         fprintf(stderr,"\nDbytes > Tbytes in rec %lu - record discarded\n",recnum);
  242.         return;
  243.         }
  244.     if (u_dbytes > nd_dbytes) {
  245.         fprintf(stderr,"\nUbytes > ndbytes in rec %lu - fixed\n",recnum);
  246.         nd_dbytes = u_dbytes;
  247.         }
  248.  
  249.     find_call(to);
  250.     total[cnum].rx_tbytes += t_bytes;
  251.     total[cnum].rx_udbytes += u_dbytes;
  252.     total[cnum].rx_ndbytes += nd_dbytes;
  253.     total[cnum].rx_tdbytes += t_dbytes;
  254.  
  255.     find_call(from);
  256.  
  257.     total[cnum].tx_tbytes += t_bytes;
  258.     total[cnum].tx_udbytes += u_dbytes;
  259.     total[cnum].tx_ndbytes += nd_dbytes;
  260.     total[cnum].tx_tdbytes += t_dbytes;
  261.  
  262. }
  263.  
  264.  
  265. do_digi()
  266. {
  267.     unsigned long packets,bytes;
  268.     char call[10];
  269.  
  270.     fscanf(stdin,",%[^,],%ld,%ld\n",call,&packets,&bytes);
  271.     find_digi(call);
  272.     digi_tab[dnum].bytes += bytes;
  273.     digi_tab[dnum].packets += packets;    
  274. }
  275.  
  276.  
  277. find_call(call)
  278. char *call;
  279. {
  280.     int i;
  281.  
  282.     for(i = 0; i < nr_calls; i++){
  283.         if(!strcmp(total[i].call,call)){
  284.             cnum = i;
  285.             return;
  286.         }
  287.     }
  288.     if(i == MAX_CIRCUIT){
  289.         fprintf(stderr,"Fatal error: circuit table is full!\n");
  290.         exit();
  291.     }
  292.     nr_calls++;
  293.     fprintf(stderr,"\r%-3d %-9s",nr_calls,call);
  294.     cnum = i;
  295.     strcpy(total[i].call,call);
  296.     total[i].rx_tbytes =
  297.     total[i].rx_udbytes =
  298.     total[i].rx_ndbytes =
  299.     total[i].rx_tdbytes =
  300.     total[i].tx_tbytes =
  301.     total[i].tx_udbytes =
  302.     total[i].tx_ndbytes =
  303.     total[i].tx_tdbytes = 0l;
  304.  
  305. }
  306.  
  307. find_digi(call)
  308. char *call;
  309. {
  310.     int i;
  311.  
  312.     for(i = 0; i < nr_digis; i++){
  313.         if(!strcmp(digi_tab[i].call,call)){
  314.             dnum = i;
  315.             return;
  316.         }
  317.     }
  318.     if(i == MAX_DIGIS){
  319.         fprintf(stderr,"Fatal error: digi table is full!\n");
  320.         exit();
  321.     }
  322.     nr_digis++;
  323.     strcpy(digi_tab[i].call,call);
  324.     dnum = i;
  325.     digi_tab[i].bytes =
  326.     digi_tab[i].packets = 0l;
  327. }
  328.  
  329. int compare(arg1,arg2)
  330. struct TOTALS *arg1,*arg2;
  331. {
  332.     long tmp;
  333.     if (sel_call) tmp = arg1->rx_tbytes - arg2->rx_tbytes;
  334.     else tmp = arg1->tx_tbytes - arg2->tx_tbytes;
  335.  
  336.     if(!tmp)
  337.         return 0;
  338.  
  339.     if(tmp < 0)         /* sort in reverse order */
  340.         return 1;
  341.     else
  342.         return -1;
  343. }
  344.  
  345. int d_compare(arg1,arg2)
  346. struct DIGI_TOTALS *arg1,*arg2;
  347. {
  348.     long tmp;
  349.     tmp = arg1->bytes - arg2->bytes;
  350.  
  351.     if(!tmp)
  352.         return 0;
  353.  
  354.     if(tmp < 0)         /* sort in reverse order */
  355.         return 1;
  356.     else
  357.         return -1;
  358. }
  359.